package com.sad.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sad.model.SystemUser;

/**
 * 
 * @author zhaoqy
 *
 */
public class LoginController extends HttpServlet {

	private static final long serialVersionUID = -564222374419890280L;

	Logger logger = LoggerFactory.getLogger(LoginController.class);

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException,
			ServletException {

		String username = req.getParameter("username");
		String password = req.getParameter("password");

		if (username != null && password != null) {
			SystemUser user = new SystemUser();
			user.setUserId("1");
			user.setUserName("admin");
			user.setPassword("123123");

			if (user != null && user.getUserName() != null && !"".equals(user.getUserName())) {
				req.getSession().setAttribute("user", user);

				resp.sendRedirect("render?pageid=wish");

			} else {
				try {
					req.setAttribute("message", "1");
					req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
				} catch (ServletException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} else {
			try {
				req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 
	 */
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException,
			ServletException {

		try {
			req.getRequestDispatcher("/pages/login.jsp").forward(req, resp);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
